package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.CheckGroupService;
import com.itheima.contant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupVS;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/checkGroup")
@RestController
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /*分页查询*/
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.findPage(queryPageBean);
    }

    /*添加检查组*/
    @RequestMapping("/addCheckGroup")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.addCheckGroup(checkGroup);
            return Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /*查询指定id的检查组*/
    @RequestMapping("/queryCheckGroup")
    public Result queryCheckGroup(Integer id) {
        try {
            CheckGroupVS checkGroupVS = checkGroupService.queryCheckGroup(id);
            return Result.success("", checkGroupVS);
        } catch (Exception e) {
            return Result.error("");
        }
    }

    /*编辑检查组*/
    @RequestMapping("/editCheckGroup")
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.editCheckGroup(checkGroup);
            return Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /*删除检查组*/
    @RequestMapping("/deleteCheckGroup")
    public Result deleteCheckGroup(Integer id) {
        try {
            checkGroupService.deleteCheckGroup(id);
            return Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return Result.success("",checkGroupList);
        } catch (Exception e) {
            return Result.error("");
        }
    }

}